package com.qoupon.client;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDataBaseHelper extends SQLiteOpenHelper {
        private final static String t ="PushListDB";
        
        private SQLiteDatabase mDB = null;
        private Cursor cursor=null;
        
        // PushData Table Columns names
        private static final String KEY_ID = "id";
        private static final String KEY_MESSAGE_ID = "messageId";
        private static final String KEY_COMPANY_NAME = "companyName";
        private static final String KEY_MESSAGE_JSON = "messageJson";
        private static final String KEY_IS_READ= "isRead";
        
        
    private static final String DATABASE_NAME = "0000000000000001.db";
    private static final String DATABASE_TABLE = "pushtable";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_PATH = "data/data/com.qoupon.client/app_database/file__0/";
    //Absolute db path
    private static final String DB_PATH = DATABASE_PATH + DATABASE_NAME;
    private static final String DATABASE_CREATE =
                  "create table pushtable (id integer primary key autoincrement, " 
                  + "messageId text not null,"
                  + "companyName text not null,"
                  + "messageJson text not null," 
                  + "isRead text not null);";

        public MyDataBaseHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
                createDatabase();
        }

        @Override
        public void onCreate(SQLiteDatabase sqlitedatabase) {
                
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqlitedatabase, int i, int j) {
                
        }
        

        private void createDatabase() {
              File dbFile = new File(DATABASE_PATH);
              if(!dbFile.exists()){
            	  dbFile.mkdirs();
              }
                try{
                        mDB = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
                        mDB.execSQL(DATABASE_CREATE);
                      
                         Log.i(t,"JobListDB data inserted");
                }catch(SQLiteException e){
                        Log.e(t,"create database got SQLite Exception: "+e);
                        
                }catch(Exception e){
                        Log.e(t,"create database got Exception: "+e);
                }finally{
                        if(mDB != null)mDB.close(); 
                }
        }

        // Adding new PushData
        public void addPushData(PushData mPush) {
             ContentValues values = new ContentValues();
             values.put(KEY_MESSAGE_ID, mPush.get_message_id()); // Contact Name
             values.put(KEY_COMPANY_NAME, mPush.get_company_name());
             values.put(KEY_MESSAGE_JSON, mPush.get_message_json()); // Contact Phone
             values.put(KEY_IS_READ, mPush.get_isRead()); // Contact Phone
              
             mDB = SQLiteDatabase.openDatabase(DB_PATH, null,SQLiteDatabase.OPEN_READWRITE);
             // Inserting Row
             mDB.insert(DATABASE_TABLE, null, values);
             if(mDB != null)mDB.close();  // Closing database connection
         }
      
         // Getting single PushData
        public PushData getPushData(int id) {
            mDB = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
             Cursor cursor = mDB.query(DATABASE_TABLE, new String[] { KEY_ID,
             		KEY_MESSAGE_ID,KEY_COMPANY_NAME, KEY_MESSAGE_JSON, KEY_IS_READ }, KEY_ID + "=?",
                     new String[] { String.valueOf(id) }, null, null, null, null);
             if (cursor != null)
                 cursor.moveToFirst();
      
             PushData contact = new PushData(Integer.parseInt(cursor.getString(0)),
                     cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
             // return contact
             return contact;
         }
          
         // Getting All PushData
         public List<PushData> getAllPushData() {
             List<PushData> PushDataList = new ArrayList<PushData>();
             // Select All Query
             String selectQuery = "SELECT  * FROM " + DATABASE_TABLE;
      
             mDB = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
             Cursor cursor = mDB.rawQuery(selectQuery, null);
      
             // looping through all rows and adding to list
             if (cursor.moveToFirst()) {
                 do {
                 	PushData mPushData = new PushData();
                 	mPushData.set_id(Integer.parseInt(cursor.getString(0)));
                 	mPushData.set_message_id(cursor.getString(1));
                 	mPushData.set_company_name(cursor.getString(2));
                 	mPushData.set_message_json(cursor.getString(3));
                	mPushData.set_isRead(cursor.getString(4));
                    
                     // Adding contact to list
                 	PushDataList.add(mPushData);
                 } while (cursor.moveToNext());
             }
      
             // return PushData list
             return PushDataList;
         }
}
